package cn.kgc.crowdfundingadmin.controller;

import cn.kgc.crowdfundingadmin.common.ResultEntity;
import cn.kgc.crowdfundingadmin.pojo.Cert;
import cn.kgc.crowdfundingadmin.pojo.Role;
import cn.kgc.crowdfundingadmin.service.CertMservice;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class CertController {
@Resource
    CertMservice certMservice;

    @ResponseBody
    @RequestMapping("/cert/search/by/keyword")
    public ResultEntity<PageInfo<Cert>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {
        //1.查询得到 pageInfo对象
        PageInfo<Cert> pageInfo = certMservice.queryForKeywordWithPage(pageNum, pageSize, keyword);
        //2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }



    //删除显示数据
    @ResponseBody
    @RequestMapping("/cert/get/list/by/id/list")
    public ResultEntity<List<Cert>> getRoleListByIdList(@RequestBody List<Integer> roleIdList) {
        List<Cert> roleList = certMservice.getRoleListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleList);
    }




    //删除
    @ResponseBody
    @RequestMapping("/cert/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList) {
        //调用service删除
        certMservice.batchRemove(roleIdList);

        return ResultEntity.successWithoutData();
    }


    @ResponseBody
    @RequestMapping("/cert/save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {
        //调用srvice
        certMservice.saveRole(roleName);

        return ResultEntity.successWithoutData();
    }



    @ResponseBody
    @RequestMapping("/cert/update/role")
    public ResultEntity<String> updateRole(Cert role) {

        certMservice.updateRole(role);

        return ResultEntity.successWithoutData();
    }


}

